
package com.insigma.business.qrcode.soldier._consumer.impl;

import com.google.common.cache.Cache;

import com.insigma.business.qrcode.common.template.MemCacheManager;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.SoldierDTO;
import com.insigma.business.qrcode.soldier.dto.SoldierOuterLoginAccountDTO;
import com.insigma.business.qrcode.soldier.request.BaseApiRequest;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier._consumer.SoldierConsumer;
import com.insigma.business.qrcode.soldier._provider.SoldierOuterLoginAccountProvider;
import com.insigma.business.qrcode.soldier._provider.SoldierProvider;
import com.insigma.business.qrcode.welfare.dto.SoldierUserInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;


@Slf4j
@Service("soldierConsumer")
public class SoldierConsumerImpl implements SoldierConsumer {

    @Resource
    private SoldierProvider soldierProvider;

    @Resource
    SoldierOuterLoginAccountProvider soldierOuterLoginAccountProvider;

    private String outerKey = "outer_soldier_id_";

    @Override
    public BaseResponse<SoldierDTO> getSoldier(final BaseGetRequest baseGetRequest) {
        return soldierProvider.getSoldier(baseGetRequest);
    }

    @Override
    public BaseResponse<Integer> deleteSoldier(BaseDeleteRequest deleteRequest) {
        return null;
    }


    @Override
    public BaseResponse<SoldierUserInfoDTO> checkSshUserInfo(String soldierId) {
        return soldierProvider.checkSshUserInfo(soldierId);
    }

    @Override
    public BaseResponse<List<SoldierUserInfoDTO>> querySoldierRelationshipList(String soldierId) {
        return soldierProvider.querySoldierRelationshipList(soldierId);
    }

    @Override
    public BaseResponse<SoldierUserInfoDTO> querySoldierDetail(String soldierId) {
        return soldierProvider.querySoldierDetail(soldierId);
    }

    @Override
    public BaseResponse<SoldierUserInfoDTO> getSoldierBasicInfoByQr(String qrString) {
        return soldierProvider.querySoldierDetailByQrString(qrString);
    }

    @Override
    public BaseResponse<SoldierOuterLoginAccountDTO> getSoldierOuterLoginAccountBySoldierId(String soldierId) {
        return soldierOuterLoginAccountProvider.getSoldierOuterLoginAccountBySoldierId(soldierId);
    }


    @Override
    public BaseResponse<SoldierUserInfoDTO> checkLbmInfo(String userId) {
        return soldierProvider.checkLbmInfo(userId);
    }

    @Override
    public BaseResponse<Boolean> updateRealNameIfMobileExisted(String id, String mobile, String userName) {
        return soldierProvider.updateRealNameIfMobileExisted(id, mobile, userName);
    }

    @Override
    public SoldierOuterLoginAccountDTO getCachedOuterAccountBySoldierId(String soldierId) {
        if (StringUtils.isBlank(soldierId)) {
            return null;
        }

        Cache<String, Object> instance = MemCacheManager.getInstance();
        String key = outerKey + soldierId;
        try {
            Object o = instance.get(key, new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    BaseResponse<SoldierOuterLoginAccountDTO> remote = getSoldierOuterLoginAccountBySoldierId(soldierId);
                    SoldierOuterLoginAccountDTO dto = remote.getDto();
                    if (dto == null) {
                        dto = new SoldierOuterLoginAccountDTO();
                    }
                    return dto;
                }
            });
            if (o != null) {
                return (SoldierOuterLoginAccountDTO) o;
            }

        } catch (ExecutionException e) {
            log.error("查询失败：{}", e);
        }
        return null;    }

    @Override
    public BaseResponse<Integer> updateBaseInfo(String soldierId, String name, String idCardNo, String isCert, String mobile) {
        return null;
    }

    @Override
    public BaseResponse<SoldierUserInfoDTO> checkSshUserInfoOldQrString(String userId) {
        return soldierProvider.checkSshUserInfoOldQrString(userId);
    }

}
